package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.Help;
import org.example.system.entity.OperateLog;
import org.example.system.entity.User;
import org.example.system.mapper.HelpMapper;
import org.example.system.service.HelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 求助服务层实现类
 */
@Service
public class HelpServiceImpl implements HelpService {
    @Autowired
    private HelpMapper helpMapper;

    /**
     * 按条件分页查询
     * @param help
     * @return
     */
    @Override
    public IPage<Help> selectPage(Help help){
        MPJLambdaWrapper<Help> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Help.class);
        wrapper.leftJoin(User.class,User::getId,Help::getUserId);
        //条件
        if (help.getUserId() != null){
            wrapper.eq(Help::getUserId,help.getUserId());
        }
        wrapper.selectAs(User::getUsername,Help::getUsername);
        if (StrUtil.isNotEmpty(help.getUsername())){
            wrapper.like(User::getUsername,help.getUsername());    //相似的用户名（模糊查询）
        }
        wrapper.selectAs(User::getRealname,Help::getRealname);
        if (StrUtil.isNotEmpty(help.getRealname())){
            wrapper.like(User::getRealname,help.getRealname());    //相似的真实姓名（模糊查询）
        }
        if (StrUtil.isNotEmpty(help.getTitle())){
            wrapper.like(Help::getTitle,help.getTitle());    //相似的求助标题（模糊查询）
        }
        if (StrUtil.isNotEmpty(help.getCreateBy())){
            wrapper.like(Help::getCreateBy,help.getCreateBy());    //相似的发布用户名（模糊查询）
        }
        if (StrUtil.isNotEmpty(help.getUpdateBy())){
            wrapper.like(Help::getUpdateBy,help.getUpdateBy());    //相似的最后编辑用户名（模糊查询）
        }
        wrapper.orderByDesc(OperateLog::getId);
        IPage<Help> page = new Page<>();
        page.setCurrent(help.getPageNum());
        page.setSize(help.getPageSize());
        return helpMapper.selectPage(page,wrapper);
    }

    /**
     * 添加求助
     * @param help
     * @return
     */
    @Override
    public int add(Help help) {
        help.setCreateTime(new Date());
        return helpMapper.insert(help);
    }

    /**
     * 修改
     * @param help
     * @return
     */
    @Override
    public int update(Help help) {
        help.setUpdateTime(new Date());
        return helpMapper.updateById(help);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return helpMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Help getOne(Long id) {
        Help help = helpMapper.selectById(id);
        MPJLambdaWrapper<Help> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Help.class);
        wrapper.leftJoin(User.class,User::getId,Help::getUserId);
        wrapper.eq(Help::getId,help.getId());
        wrapper.eq(Help::getUserId,help.getUserId());
        wrapper.selectAs(User::getRealname,Help::getRealname);
        wrapper.orderByDesc(OperateLog::getId);
        IPage<Help> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        return helpMapper.selectPage(page,wrapper).getRecords().get(0);
    }
}
